package com.zk.viewcodes.activity;

import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.zk.viewcodes.R;
import com.zk.viewcodes.ui.MBottomSheetDialogFragment;

/**
 * author: ZK.
 * date:   On 2018-12-04.
 */
public class BottomSheetActivity extends AppCompatActivity implements View.OnClickListener {


    private MBottomSheetDialogFragment mDialogFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_sheet);
        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
                View view = LayoutInflater.from(this).inflate(R.layout.layout_bottom_sheet_dialog, null);
                Display display = getWindowManager().getDefaultDisplay();
                Point point = new Point();
                display.getSize(point);
                ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(point.x,point.y);
                view.setLayoutParams(layoutParams);
                bottomSheetDialog.setContentView(view);

                BottomSheetBehavior behavior = BottomSheetBehavior.from((View) view.getParent());
                behavior.setPeekHeight(point.y * 1 / 7); // 设置折叠时的最小高度
                behavior.setHideable(false);  //当我们拖拽下拉的时候，bottom sheet是否能全部隐藏

                behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {

                    /**
                     * @param bottomSheet
                     * @param newState  STATE_DRAGGING = 1    : 拖曳
                     *                  STATE_SETTLING = 2    : 固定
                     *                  STATE_EXPANDED = 3    : 完全展开
                     *                  STATE_COLLAPSED = 4   ：折叠
                     *                  STATE_HIDDEN = 5      ：隐藏
                     */
                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {
                        switch (newState) {
                            case BottomSheetBehavior.STATE_DRAGGING:
                                Log.d("=======onStateChanged", "拖曳");
                                break;
                            case BottomSheetBehavior.STATE_EXPANDED:
                                Log.d("=======onStateChanged", "完全展开");
                                break;
                            case BottomSheetBehavior.STATE_COLLAPSED:
                                Log.d("=======onStateChanged", "折叠");
                                break;
                            case BottomSheetBehavior.STATE_HIDDEN:
                                bottomSheetDialog.dismiss();//  隐藏状态是指内容View隐藏了，所以下滑隐藏时，还会有一个黑色透明的遮罩层
                                break;
                        }
                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                        Log.d("=======onSlide", slideOffset + "");
                    }
                });


                bottomSheetDialog.show();
                break;
            case R.id.btn2:
                if (mDialogFragment == null)
                    mDialogFragment = new MBottomSheetDialogFragment();
                mDialogFragment.show(getSupportFragmentManager(), MBottomSheetDialogFragment.class.getName());
                break;
        }
    }
}
